package com.sh3h.generator.web.domain.query.entity;

import com.sh3h.generator.web.domain.GenDataSource;
import com.sh3h.generator.web.domain.query.ITableQuery;
import com.sh3h.generator.web.domain.query.MySqlTableQuery;
import com.sh3h.generator.web.domain.query.SqlServerTableQuery;
import lombok.Data;

import java.util.Date;

@Data
public class TableQuery {
    /**
     * id
     */
    private Long id;

    /**
     * 数据库类型
     */
    private String dbType;

    /**
     * 连接名
     */
    private String connName;

    /**
     * URL
     */
    private String connUrl;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    private String driverClass;

    private static final long serialVersionUID = 1L;
    private ITableQuery query;

    public TableQuery(GenDataSource dataSource) {
        this.setId(dataSource.getId());
        this.setConnName(dataSource.getConnName());
        this.setPassword(dataSource.getPassword());
        this.setUsername(dataSource.getUsername());
        this.setConnUrl(dataSource.getConnUrl());
        this.setDbType(dataSource.getDbType());
        switch (dataSource.getDbType()) {
            case "MySql":
                driverClass = "com.mysql.cj.jdbc.Driver";
                query = new MySqlTableQuery();
                break;
            case "SqlServer":
                driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                query = new SqlServerTableQuery();
                break;
            case "Oracle":
                driverClass = "oracle.jdbc.driver.OracleDriver";
                break;
            case "DM":
                driverClass = "dm.jdbc.driver.DmDriver";
                break;
            case "KingBase":
                driverClass = "com.kingbase8.Driver";
                break;
            default:
                break;
        }
        this.setDriverClass(driverClass);
    }
}
